package com.szqhyxj.youxiangju.biz.login;

import android.text.TextUtils;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.biz.core.TokenManager;
import com.szqhyxj.youxiangju.biz.core.UserInfo;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.core.UserInfoModel;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.umeng.analytics.MobclickAgent;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.OnErrorNotImplementedException;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

class LoginPresenter implements LoginContract.Presenter {

    private LoginContract.View loginView;

    private LoginModel.LoginEntity loginEntity;

    private Disposable disposable;

    LoginPresenter(LoginContract.View loginView) {
        this.loginView = loginView;
        this.loginEntity = new LoginModel.LoginEntity();
    }

    @Override
    public void login() {
        if (TextUtils.isEmpty(this.loginEntity.getPassword())) {
            // TODO: 2018/10/29 这里是否有提示
            return;
        }
        if (TextUtils.isEmpty(this.loginEntity.getAccount())) {
            // TODO: 2018/10/29 这里是否有提示
            return;
        }
        disposable = LoginModel
                .login(loginEntity)
                .subscribeOn(Schedulers.io())
                .flatMap(
                        new Function<BaseResult<LoginModel.LoginResult>, ObservableSource<BaseResult<UserInfo>>>() {
                            @Override
                            public ObservableSource<BaseResult<UserInfo>> apply(BaseResult<LoginModel.LoginResult> loginResult) {
                                if (loginResult.businessLogicSuccess()) {
                                    TokenManager.INSTANCE.setToken(loginResult.getData().getToken());
                                    return UserInfoModel.loadUserInfo();
                                } else {
                                  return Observable.error(new Exception(loginResult.getMessage()));
                                }
                            }
                        }
                )
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new BaseConsumer<UserInfo>() {
                            @Override
                            protected void error(String code, String msg){
                                loginView.showLoginFail(msg);
                            }

                            @Override
                            protected void successAccept(UserInfo userInfoBaseResult){
                                UserInfoManger.INSTANCE.setUser(userInfoBaseResult);
                                loginView.showLoginSuccess();
                                loginView.showLoginFail("登录成功");

                                MobclickAgent.onProfileSignIn(userInfoBaseResult.getUseraccount()+"");
                            }

                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) {
                                if (throwable instanceof OnErrorNotImplementedException){
                                    loginView.showLoginFail("网络连接异常");
                                }else {
                                    loginView.showLoginFail(throwable.getMessage());
                                }

                            }
                        }
                );
    }

    @Override
    public void register() {
        loginView.navToRegister();
    }

    @Override
    public void forgetPassword() {
        loginView.navToForgetPassword();
    }

    @Override
    public void setAccount(String account) {
        this.loginEntity.setAccount(account);
        if (!TextUtils.isEmpty(loginEntity.getAccount()) && !TextUtils.isEmpty(loginEntity.getPassword())) {
            loginView.showLoginEnable();
        } else {
            loginView.showLoginDisable();
        }
    }

    @Override
    public void setPassword(String password) {
        this.loginEntity.setPassword(password);
        if (!TextUtils.isEmpty(loginEntity.getAccount()) && !TextUtils.isEmpty(loginEntity.getPassword())) {
            loginView.showLoginEnable();
        } else {
            loginView.showLoginDisable();
        }
    }

    @Override
    public void onDestroy() {
        if (disposable != null) {
            disposable.dispose();
        }
    }
}